package cen5501c.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FilePartitioner {
	public static File getPortion(File file, int start, int end){
		try{
			FileInputStream fis=new FileInputStream(file);
			fis.skip(start);
			File nFile=new File("files/"+file.getName()+"_"+start+"_"+end);
			FileOutputStream fos=new FileOutputStream(nFile);
			byte [] buffer=new byte[1024];
			int len=0;
			int byteNum=end-start+1;
			while(byteNum>0&&(len=fis.read(buffer,0,byteNum))!=-1){
				fos.write(buffer, 0, len);
				byteNum-=len;
			}
			fis.close();
			fos.close();
			return nFile;
		}catch(IOException ie){
			ie.printStackTrace();
			return null;
		}
	}
	public static void main(String[] args){
		FilePartitioner.getPortion(new File("files/testfile.txt"), 4, 10);
	}
}
